/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.im.sinocab.auxController;

import com.mx.im.sinocab.bean.Menu;
import com.mx.im.sinocab.bean.Option;
import com.mx.im.sinocab.bean.User;
import com.mx.im.sinocab.dao.AdminMenuDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Javier Aviles Cruz
 */
public class AdminMenuActionAC {

    private AdminMenuDAO amd = new AdminMenuDAO();
    private static ArrayList<Menu> menusKnow;
    private static ArrayList finalMenus = new ArrayList();

    public ArrayList listMenuOptions() {
        return menuClasification(amd.listMenuOptions());
    }

    public ArrayList<Menu> getMenus() {
        return groupMenus(amd.listMenuOptions());
    }

    private static ArrayList menuClasification(ArrayList<Option> listMenuOptions) {
        ArrayList menuOption;
        ArrayList<Option> options;
        //ArrayList menuData;
        finalMenus = new ArrayList();
        menusKnow = groupMenus(listMenuOptions);
        for (Menu menu : menusKnow) {
            //menuData = (ArrayList) optionString;
            options = new ArrayList<Option>();
            menuOption = new ArrayList();
            for (Option option : listMenuOptions) {
                if (option.getMenu().getIdMenu() == menu.getIdMenu()) {
                    options.add(option);
                }
            }
            menuOption.add(menu);
            menuOption.add(options);
            finalMenus.add(menuOption);
        }
        return finalMenus;
    }

    private static ArrayList groupMenus(ArrayList<Option> listMenuOptions) {
        //ArrayList menuData;
        menusKnow = new ArrayList<Menu>();
        for (Option option : listMenuOptions) {
            if (!isItKnown(option.getMenu().getIdMenu())) {
                //menuData = new ArrayList();
                //menuData.add(option.getMenu().getIdMenu().toString());
                //menuData.add(option.getMenu().getMenu().toString());
                menusKnow.add(option.getMenu());
            }
        }
        return menusKnow;
    }

    private static boolean isItKnown(int idMenu) {
        if (menusKnow.isEmpty()) {
            return false;
        } else {
            //ArrayList menuData;
            for (Menu menu : menusKnow) {
                //menuData = (ArrayList) menuKnow;

                if (menu.getIdMenu() == idMenu) {
                    return true;
                }
            }
            return false;
        }
    }

    public Map<String, Object> setNewMenuOption(Map<String, String[]> formData, User userLogin) {
        Map<String, Object> resultMessages = new HashMap<String, Object>();
        if (!formData.isEmpty()) {
            amd = new AdminMenuDAO();
            for (Map.Entry<String, String[]> entry : formData.entrySet()) {
                System.out.println("paramName: " + entry.getKey());
                if (entry.getKey().indexOf("newOption") != -1) {
                    Option option;
                    for (int j = 0; j < entry.getValue().length; j++) {
                        System.out.println("-----> Value: " + entry.getValue()[j]);
                        try {
                            if (j != 0) {
                                if ((j + 1) % 5 == 0) {
                                    option = new Option(
                                            0,
                                            userLogin,
                                            new Menu(Integer.valueOf(entry.getValue()[j])),
                                            entry.getValue()[j - 3],
                                            entry.getValue()[j - 2],
                                            entry.getValue()[j - 1],
                                            entry.getValue()[j - 4].toLowerCase().equals("on") ? true : false);
                                    resultMessages.put("Estado del registro para '" + option.getOption() + "': " + (amd.insertOption(option) ? "<font style='color: blue; '>Registro correcto</font>" : "<font style='color: red; '>Registro incorrecto</font>"), option);
                                }
                            }
                        } catch (SQLException sqle) {
                            System.out.println("Error: " + sqle.getMessage());
                            sqle.printStackTrace();
                        } catch (Exception e) {
                            System.out.println("Error: " + e.getMessage());
                            e.printStackTrace();
                        } 
                    }
                } else {
                    if (entry.getKey().indexOf("newMenu") != -1) {
                        Menu menu;
                        for (int j = 0; j < entry.getValue().length; j++) {
                            System.out.println("-----> Value: " + entry.getValue()[j]);
                            try {
                                if (j != 0) {
                                    if ((j + 1) % 3 == 0) {
                                        menu = new Menu(0, userLogin, entry.getValue()[j - 1], entry.getValue()[j],
                                                entry.getValue()[j - 2].equals("on") ? true : false);
                                        System.out.println("Data: ----> " + menu.getMenu() + " - " + menu.getDescription() + " - " + menu.isStatus());
                                        resultMessages.put("Registrar menú --> " + menu.getMenu() + ": " + String.valueOf(amd.insertMenu(menu)), menu);
                                    }
                                }
                            } catch (SQLException sqle) {
                                System.out.println("Error: " + sqle.getMessage());
                                sqle.printStackTrace();
                            } catch (Exception e) {
                                System.out.println("Error: " + e.getMessage());
                                e.printStackTrace();
                            }
                        }
                    } else {
                        ////// lo de los checks que ya estaban
                    }
                }
            }
        } else {
        }
        return resultMessages;
    }
}
